#include "SelectionBox.h"


SelectionBox::SelectionBox()
{
	addDrawable( CreateGeometry() );
}


SelectionBox::~SelectionBox()
{
}


osg::Geometry* SelectionBox::CreateGeometry()
{
	static float s = 0.8f;

	// Create an object to store geometry in.
	osg::Geometry* geom = new osg::Geometry;

	// set colors
	osg::Vec4Array* c = new osg::Vec4Array;
    c->push_back( osg::Vec4( 1., 1., 1., 1. ) );
    geom->setColorArray( c );
    geom->setColorBinding( osg::Geometry::BIND_OVERALL );
	geom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

	// add vertices
	osg::Vec3Array* v = new osg::Vec3Array;
	geom->setVertexArray( v );
	for(int i=0; i<8; i++)
	{
		float x = i & 1 ? -1 : 1;
		float y = i & 2 ? -1 : 1;
		float z = i & 4 ? -1 : 1;
		v->push_back( osg::Vec3(x,     y,     z) );
		v->push_back( osg::Vec3(x * s, y,     z) );
		v->push_back( osg::Vec3(x,     y * s, z) );
		v->push_back( osg::Vec3(x,     y,     z * s) );
	}

	// add indices
	GLushort idxLines[6] = { 0, 1, 0, 2, 0, 3 };
	for(int j=0; j<8; j++)
	{
		geom->addPrimitiveSet( new osg::DrawElementsUShort(osg::PrimitiveSet::LINES, 6, idxLines) );
		for(int i=0; i<6; i++)
		{
			idxLines[i] += 4;
		}
	}

	return geom;
}